package com.hd.trans.files.adapter;

import android.util.Pair;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import com.hd.trans.R;
import com.hd.trans.files.bean.Video;
import com.hd.trans.files.holder.MediaPathViewHolder;
import com.hd.trans.files.sort.ISort;
import com.hd.trans.files.sort.MediaSortHelper;
import com.hd.trans.utils.DateFormatUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: yangtianwen
 * Time: 2021/12/16
 * Des: 从文件路径 视频列表
 */
public class VideoPathAdapter extends BaseAdapter<Video, MediaPathViewHolder> {

    private List<Video> mSelectedVideoList = new ArrayList<>();

    public VideoPathAdapter() {
        super(new DiffUtil.ItemCallback<Video>() {
            @Override
            public boolean areItemsTheSame(@NonNull Video oldItem, @NonNull Video newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Video oldItem, @NonNull Video newItem) {
                return false;
            }
        });
        addChildClickViewIds(R.id.file_path_cb);
    }

    @Override
    void bindViewHolder(MediaPathViewHolder holder, Video video) {
        holder.setText(R.id.file_path_tv_name, video.getName());
        holder.setText(R.id.file_path_tv_date, DateFormatUtil.getDate(video.getDate()));
        holder.showFolder(video.isDirectory());
        holder.checked(mSelectedVideoList.contains(video));
    }

    @Override
    protected void bindViewHolder(MediaPathViewHolder holder, Video video, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            this.bindViewHolder(holder, video);
        } else {
            holder.checked(mSelectedVideoList.contains(video));
        }
    }

    @Override
    MediaPathViewHolder createViewHolder(View itemView) {
        return new MediaPathViewHolder(itemView);
    }

    @Override
    int getLayoutId() {
        return R.layout.layout_item_files_path;
    }

    public void updateSelectedVideo(Pair<List<Video>, Video> videos) {
        mSelectedVideoList = videos.first;
        int position = getCurrentList().indexOf(videos.second);
        notifyItemChanged(position, "check");
    }

    public void sort(ISort.SortMethod method) {
        List<Video> sourceVideos = new ArrayList<>(getCurrentList());
        submitList(MediaSortHelper.getInstance().sortVideo(sourceVideos, method));
    }
}
